package com.example.windows8.sinaweibo;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.sina.weibo.sdk.api.TextObject;
import com.sina.weibo.sdk.api.WeiboMultiMessage;
import com.sina.weibo.sdk.api.share.IWeiboShareAPI;
import com.sina.weibo.sdk.api.share.SendMultiMessageToWeiboRequest;
import com.sina.weibo.sdk.api.share.WeiboShareSDK;
import com.sina.weibo.sdk.auth.AuthInfo;
import com.sina.weibo.sdk.auth.Oauth2AccessToken;
import com.sina.weibo.sdk.auth.WeiboAuthListener;

import com.sina.weibo.sdk.exception.WeiboException;
import com.sina.weibo.sdk.net.RequestListener;
import com.sina.weibo.sdk.utils.LogUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class SinaWeibo extends AppCompatActivity implements View.OnClickListener {
    /** 当前 Token 信息 */
    private Oauth2AccessToken mAccessToken;
    /** 用于获取微博信息流等操作的API */
    private StatusesAPI mStatusesAPI;
    /** 获取应用名 */
    private static final String TAG = WBStatusAPIActivity.class.getName();

    /** 获取微博 */
    private Map<String, Object> item;
    private ListView list;

    /** 发布微博 */
    private EditText editText;


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sina_weibo);

        Button button = (Button)findViewById(R.id.button);
        button.setOnClickListener(this);
        ArrayList<Map<String,Object>> mylist = new ArrayList<Map<String,Object>>();
        item = new HashMap<String, Object>();
        list = (ListView)findViewById(R.id.listView);
        editText = (EditText)findViewById(R.id.editText);
        SimpleAdapter adapter = new SimpleAdapter(SinaWeibo.this, mylist, R.layout.iconlist,
                            new String[]{"image","title","text"},
                            new int[]{R.id.image, R.id.title, R.id.text} );
        list.setAdapter(adapter);
        //生成适配器，数组===》ListItem

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        toolbar.setOnMenuItemClickListener(onMenuItemClick);

        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

        // 获取当前已保存过的 Token
        mAccessToken = AccessTokenKeeper.readAccessToken(this);
        // 对statusAPI实例化
        mStatusesAPI = new StatusesAPI(this, Constants.APP_KEY, mAccessToken);
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    /*
    * 中间button
     */
    public void onClick(View v) {
        int id = v.getId();
        switch(id){
            case R.id.button:
                String text = editText.getText().toString();
                editText.setText("");
                mStatusesAPI.update(text, null, null, mListener);
                break;

        }
    }

//////////////////////////////////////////////////////底部导航栏///////////////////////////////////////////
    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    mStatusesAPI.friendsTimeline(0L, 0L, 10, 1, false, 0, false, mListener);



                    return true;
                case R.id.navigation_dashboard:
                    return true;
                case R.id.navigation_notifications:
                    return true;
                case R.id.navigation_more:
                    return true;
                case R.id.navigation_myself:
                    return true;
            }
            return false;
        }

    };
    private IWeiboShareAPI mWeiboShareAPI;

    /////////////////////////////////////顶部工具栏///////////////////////////////////
   private Toolbar.OnMenuItemClickListener onMenuItemClick = new Toolbar.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem menuItem) {
            String msg = "";
            switch (menuItem.getItemId()) {
                case R.id.action_edit:
                    msg += "发布微博，需要高级权限";
                    break;
                case R.id.action_share:
                    msg += "转发微博，需要高级权限";
                    break;
                case R.id.action_settings:
                    msg += "Click setting";
                    break;
            }

            if(!msg.equals("")) {
                Toast.makeText(SinaWeibo.this, msg, Toast.LENGTH_SHORT).show();
            }
            return true;
        }
    };

    /**
     * 微博 OpenAPI 回调接口。
     */
    private RequestListener mListener = new RequestListener() {
        @Override
        public void onComplete(String response) {
            if (!TextUtils.isEmpty(response)) {
                LogUtil.i(TAG, response);
                if (response.startsWith("{\"statuses\"")) {
                    // 调用 StatusList#parse 解析字符串成微博列表对象
                    StatusList statuses = StatusList.parse(response);
                    if (statuses != null && statuses.total_number > 0) {
                        Toast.makeText(SinaWeibo.this,
                                "获取微博信息流成功, 条数: " + statuses.statusList.size(),
                                Toast.LENGTH_LONG).show();
                        int i = 0;
                        while(i<statuses.statusList.size()){

                            item.put("image", statuses.statusList.get(i).original_pic);
                            item.put("title", statuses.statusList.get(i).idstr);
                            item.put("text",statuses.statusList.get(i).text);
                            i++;
                        }

                    }
                } else if (response.startsWith("{\"created_at\"")) {
                    // 调用 Status#parse 解析字符串成微博对象
                    Status status = Status.parse(response);
                    Toast.makeText(SinaWeibo.this,
                            "发送一送微博成功, id = " + status.id,
                            Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(SinaWeibo.this, response, Toast.LENGTH_LONG).show();
                }
            }
        }

        @Override
        public void onWeiboException(WeiboException e) {
            LogUtil.e(TAG, e.getMessage());
            ErrorInfo info = ErrorInfo.parse(e.getMessage());
            Toast.makeText(SinaWeibo.this, info.toString(), Toast.LENGTH_LONG).show();
        }
    };

}
